<?php

namespace app\admin\controller\goods;

use app\common\controller\Backend;

/**
 * 商品管理
 *
 * @icon fa fa-circle-o
 */
class Goods extends Backend
{

    /**
     * Goods模型对象
     * @var \app\common\model\goods\Goods
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
//        $this->model = new \app\common\model\goods\Goods;
        $this->model = new \app\admin\model\goods\Goods;
        $this->view->assign("specTypeList", $this->model->getSpecTypeList());
        $this->view->assign("deductStockTypeList", $this->model->getDeductStockTypeList());
        $this->view->assign("isPostageList", $this->model->getIsPostageList());
        $this->view->assign("isDelList", $this->model->getIsDelList());
        $this->view->assign("isHotList", $this->model->getIsHotList());
        $this->view->assign("isBestList", $this->model->getIsBestList());
        $this->view->assign("isNewList", $this->model->getIsNewList());
        $this->view->assign("isBenefitList", $this->model->getIsBenefitList());
        $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
        $this->view->assign("isSeckillList", $this->model->getIsSeckillList());
        $this->view->assign("isBargainList", $this->model->getIsBargainList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['goodscategory'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['goodscategory'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {
                $row->visible(['id', 'image', 'slider_images', 'goods_name', 'spec_type', 'sales_actual', 'is_postage', 'status', 'browse']);
                $row->visible(['goodscategory']);
                $row->getRelation('goodscategory')->visible(['name']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加商品
     * @return string
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isAjax()) {
//            $row = $this->request->post('row/a');
            $data = $this->request->post();
            if ($this->model->add($data)) {
                $this->success();
            }
            //$this->error(__('unknown error.'));
            return json($data);
        }
        return $this->view->fetch();
    }

    /**
     * 商品编辑
     * @param $ids
     * @return array|mixed
     * @throws \think\exception\DbException
     */

    public function edit($ids = null)
    {
        $model = $this->model->getGoodsDetail($ids);
//        halt($model);
        if (!$this->request->isAjax()) {
            // 多规格信息
            $spec_data = null;
            if ($model['spec_type'] == 20) {
                $spec_data = $model->getManySpecData($model['spec_rel'], $model['spec']);
            }
            $this->view->assign("row", $model);
            $this->view->assign("goods_spec", $model['spec'][0]);
            $this->assignconfig('spec_data', $spec_data);
            return $this->view->fetch();
        }
        // 更新记录
        if ($model->edit($this->request->post())) {
            $this->success();
        }
        $this->error($model->getError());
    }
}
